package com.chaosj.skill.discovery.registry

import com.chaosj.config.seata.ConfigurationFactory
import com.chaosj.config.seata.ConfigurationKeys
import com.chaosj.skill.spi.seata.EnhancedServiceLoader

/**
 * @Description:
 * @author: caoyangjie
 * @date: 2019年12月2019/12/23日 17:06
 * @Copyright: © 赢时胜
 */
class RegistryFactory {
    static IRegistryService getInstance(){
        RegistryType registryType
        String registryTypeName = ConfigurationFactory.CURRENT_FILE_INSTANCE.getConfig(
                ConfigurationKeys.FILE_ROOT_REGISTRY + ConfigurationKeys.FILE_CONFIG_SPLIT_CHAR
                        + ConfigurationKeys.FILE_ROOT_TYPE)
        try {
            registryType = RegistryType.getType(registryTypeName)
        } catch (Exception exx) {
            throw new RuntimeException("not support registry type: " + registryTypeName)
        }
        if (RegistryType.File == registryType) {
            return FileRegistryServiceImpl.getInstance()
        } else {
            return EnhancedServiceLoader.load(IRegistryProvider.class, Objects.requireNonNull(registryType).name()).provide()
        }
    }
}
